//
//  UIViewController+Router.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/9.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation



fileprivate extension QDStationsViewController.QDStationsType {
    
    /// 时间描述
    var eventDescription: String {
        switch self {
        case .select: return "选择模式"
        case .search: return "查看模式"
        }
    }
}


extension String {
    
    /// 方法key
    var functionPlatformKey: UIViewController.PlatformKey {
        switch self {
        /* 2.1.0 版本开始弃用，顺序启用新的标志 */
//        case "13": return .stations(type: .search, delegate: nil)
//        case "20": return .news
//        case "3": return .life
//        case "-1": return .lineGraph(delegate: nil, type: .other)
        /* 弃用结束 */
        case "0": print("线网图"); return .lineGraph(delegate: nil, type: .other) //线网图
        case "1": print("生活圈"); return .life //生活圈
        case "2": print("站点信息"); return .stations(type: .search, delegate: nil) //站点
        case "3": print("资讯"); return .news //地铁新闻
        case "4": print("借吧");  return .none //借吧
        case "5": print("网页"); return .web //网页
        case "6": print("理财吧"); return .none //理财吧
        case "7": print("指南"); return .guide //指南
        case "8": print("乘客服务"); return .userService //乘客服务
        case "9": print("360全景"); return .fullView3D //3D全景
        default: return .none
        }
    }
}




/// 进行拓展
extension UIViewController {
    
    /// 平台key
    enum PlatformKey {
        /// 消息分类列表
        case messageIndex
        /// 消息详细列表
        case messageList(type: String, typeName: String,hasUnread: Bool, readComplete: (()->())?)
        /// 站点信息
        case stations(type: QDStationsViewController.QDStationsType, delegate: QDStationsViewSelectAction?)
        /// 站点详情
        case stationDetail(id: String, name: String, fromMap: Bool, delegate: QDStationDetailViewControllerMapAction?)
        /// 资讯
        case news
        /// 生活圈
        case life
        /// 生活圈中间的功能键
        case lifeFunction(id: String, title: String?)
        /// 线网图
        case lineGraph(delegate: QDLineGraphViewControllerPurchaseDelegate?,type: QDLineGraphViewController.ActionType)
        /// 公交车线路
        case busLines
        /// 地铁-自助补站
        case metroSelfService
        /// 二维码乘车设置
        case codeSetting
        /// 绑定支付方式
        case payTypeList
        /// 支付管理
        case payManager
        /// 我的信息
        case myInfo
        /// 登录
        case login
        /// 用户设置界面
        case setting
        /// 用户积分界面
        case grade
        /// 我的收藏
        case myCollect
        /// 地铁买票
        case metroTicket(start: QDMetroSimpleStation?, end: QDMetroSimpleStation?)
        /// 乘车记录主界面
        case record
        /// 乘客服务
        case userService
        /// 帮助与客服
        case support
        /// 反馈
        case feed(type: QDQuestionFeedViewController.QDQuestionFeedType)
        /// 网页,仅仅用于判断是否是web,使用 pushToWebViewController() or pushToShareWebViewController
        case web
        /// 指南
        case guide
        /// 3D全景
        case fullView3D
        /// 发票与报销
        case bill
        /// 无跳转
        case none
    }
    
    
    /// 进行路由跳转
    func router(platform: PlatformKey?) {
        
        guard let platform = platform else { return }
        
        print("\n\n我是 Swift router!\n\n")
        
        /// 进行跳转的控制器
        var viewController = UIViewController()
        
        // 根据平台进行跳转
        switch platform {
        
        //站点
        case .stations(let type, let delegate):
            viewController = QDStationsViewController(type: type, delegate: delegate)
            QDUMEventManager.event(event: .station, info: ["type": type.eventDescription])
            
        // 站点详情
        case .stationDetail(let id, let name, let fromMap, let delegate):
            viewController = QDStationDetailViewController(stationId: id, stationName: name, fromMap: fromMap, delegate: delegate)
            
        // 资讯列表
        case .news:
            viewController = QDNewsTableViewController()
            
        //生活圈列表
        case .life:
            viewController = QDLifeTableViewController()
            
        //生活圈中间的功能键
        case .lifeFunction(let id, let title):
            viewController = QDTextListViewController()
            viewController.navigationItem.title = title
            (viewController as! QDTextListViewController).type = id
            
        //线网图
        case .lineGraph(let delegate, let type):
            viewController = QDLineGraphViewController(delegate: delegate, type: type)
            
        //消息分类界面
        case .messageIndex:
            viewController = QDMessageMainViewController()
            
        //消息详情列表
        case .messageList(let type, let typeName, let hasUnread, let readComplete):
            viewController = QDMessageListViewController(type: type, typeName: typeName, hasUnread: hasUnread)
            (viewController as! QDMessageListViewController).readComplete = readComplete
            
        //公交开通线路
        case .busLines:
            viewController = QDBusLineTableViewController()
            
        //地铁自助补站
        case .metroSelfService:
            viewController = UIStoryboard(name: "CLDomain", bundle: Bundle.main).instantiateInitialViewController() ?? UIViewController()
            
        //二维码乘车设置
        case .codeSetting:
            viewController = QDQRCodeSettingViewController()
            
        //绑定支付方式
        case .payTypeList:
            viewController = QDPayTypeViewController()
            
        //支付管理
        case .payManager:
            viewController = QDPayManagerViewController()
            
        //我的信息
        case .myInfo:
            viewController = QDMineViewController()
            
        //个人设置
        case .setting:
            viewController = QDSettingViewController()
            
        //个人积分
        case .grade:
            viewController = QDGradeViewController()
            
        //我的收藏
        case .myCollect:
            viewController = QDMyCollectListViewController()
            viewController.title = "我的收藏"
            (viewController as! QDMyCollectListViewController).url = collectListUrl
            
        //购买地铁车票
        case .metroTicket(let start, let end):
            viewController = QDMetroTicketViewController(start: start ?? QDMetroSimpleStation(),
                                                         end: end ?? QDMetroSimpleStation())
            
        //乘车记录
        case .record:
            viewController = QDRecordIndexViewController()
            
        //乘客服务
        case .userService:
            viewController = QDUserServiceViewController()
            
        //帮助与客服
        case .support:
            viewController = QDSupportViewControler()
            
        //反馈
        case .feed(let type):
            viewController = QDQuestionFeedViewController(type: type)
            
        //指南
        case .guide:
            viewController = QDGuideTableViewController()
            
        //3D全景
        case .fullView3D:
            viewController = QDFullView3DTableViewController()
            
        //发票与报销
        case .bill:
            viewController = QDMetroBillViewController()
            
        //登录
        case .login:
            checkLogin { }; return
            
        case .none, .web: return
        }
        
        
        //进行登陆判定
        switch platform {
           // 需要登陆的
        case .messageIndex,
             .messageList(_, _, _, _),
             .metroSelfService,
             .codeSetting,
             .payTypeList,
             .payManager,
             .myInfo,
             .setting,
             .grade,
             .myCollect,
             .metroTicket,
             .record,
             .feed,
             .bill:
            
            // 进行登录检测
            checkLogin {[unowned self] in
                self.ritl_topNavigationPush(viewController, animated: true);
            }
            
        default:
            ritl_topNavigationPush(viewController, animated: true)
        }
    }
}


/// 适配ObjC
extension UIViewController {
    
    /// 跳转到生活圈中间的功能键模块
    @objc func routerToLifeFunction(id: String, title: String?){
        router(platform: .lifeFunction(id: id, title: title))
    }
    
    /// 跳转到开通线路
    @objc func routerToBusLines() {
        pushToShareWebViewController(url: busUserFieldUrl, shareTitle: "公交码使用范围", title:"使用范围")
    }
    
    /// 跳转到自助补站
    @objc func routerToMetroSelfService() {
        router(platform: .metroSelfService)
    }
    
    /// 跳转到二维码设置
    @objc func routerToCodeSetting() {
        router(platform: .codeSetting)
    }
    
    /// 跳转到绑定支付界面
    @objc func routerToPayTypeList(){
        router(platform: .payTypeList)
    }
}




extension UIViewController {
    
    /// 弹出网页控制器，不使用分享
    @objc func pushToWebViewController(url: String? = "",
                                       autoTitle: Bool = false,
                                       title: String = "详情",
                                       id: String? = nil,
                                       qdmetro: Bool = false){
        
        print("\n\n我是 Swift router! Web \n\n")
        
        guard let url = url else { return }
        guard !url.isEmpty else { return }
        
        let webController = QDWebViewController()
        webController.url = url
        webController.identifier = id
        
        webController.autoTitle = autoTitle
        webController.webTitle = title
        webController.hidesBottomBarWhenPushed = true
        
        print("url = \(url)")
        ritl_topNavigationPush(webController, animated: true)
    }
    
    
    /// 弹出网页控制器，使用分享
    @objc func pushToShareWebViewController(url: String? = "",
                                            shareTitle: String,
                                            shareImageUrl: String? = nil,
                                            shareDesc: String? = nil,
                                            autoTitle: Bool = false,
                                            title: String = "详情",
                                            id: String? = nil,
                                            qdmetro: Bool = false) {
        
        print("\n\n我是 Swift router! Share Web \n\n")
        
        guard let url = url else { return }
        guard !url.isEmpty else { return }
        
        let webController = QDWebViewController()
        webController.url = url
        webController.identifier = id
        
        webController.shouldShare = true
        webController.shareTitle = shareTitle
        webController.shareDesc = shareDesc
        webController.shareImageUrl = shareImageUrl
        webController.autoTitle = autoTitle
        webController.webTitle = title
        webController.hidesBottomBarWhenPushed = true
        
        print("url = \(url)")
        ritl_topNavigationPush(webController, animated: true)
    }
}
